/**
 * 批量修复头像显示脚本
 * 将所有写死的头像路径替换为动态头像组件
 */

const fs = require('fs');
const path = require('path');

// 需要修复的文件列表
const filesToFix = [
  'ruoyi-app/pages/repair/index.vue',
  'ruoyi-app/pages/spare-parts/index.vue',
  'ruoyi-app/pages/more/index.vue',
  'ruoyi-app/pages/warehouse/dashboard.vue',
  'ruoyi-app/pages/worker/dashboard.vue'
];

// 修复单个文件
function fixAvatarInFile(filePath) {
  try {
    let content = fs.readFileSync(filePath, 'utf8');
    
    // 替换硬编码的头像
    content = content.replace(
      /<image class="avatar" src="\/static\/个人头像\.png" @click="goToProfile"><\/image>/g,
      `<UserAvatar 
        class="avatar" 
        @click="goToProfile" 
        :clickable="true"
      />`
    );
    
    // 替换动态头像（已经是正确的格式，跳过）
    // content = content.replace(
    //   /<image class="avatar" :src="userInfo\.avatar \|\| '\/static\/个人头像\.png'" @click="goToProfile"><\/image>/g,
    //   `<UserAvatar 
    //     class="avatar" 
    //     @click="goToProfile" 
    //     :clickable="true"
    //   />`
    // );
    
    // 添加组件导入
    if (content.includes('<UserAvatar') && !content.includes('UserAvatar: () => import')) {
      content = content.replace(
        /export default \{/,
        `export default {
    components: {
      UserAvatar: () => import('@/components/UserAvatar.vue')
    },`
      );
    }
    
    fs.writeFileSync(filePath, content);
    console.log(`✅ 已修复: ${filePath}`);
  } catch (error) {
    console.error(`❌ 修复失败: ${filePath}`, error.message);
  }
}

// 执行批量修复
console.log('开始批量修复头像显示...');
filesToFix.forEach(fixAvatarInFile);
console.log('头像修复完成！');
